<template>
  <div class="my-tag">
    <input
      v-if="flag"
      class="input"
      type="text"
      placeholder="输入标签"
      @blur="close"
      ref="input"
      :value="value"
      @keyup.enter="$event.target.blur()"
    />
    <div v-else class="text" @dblclick="showInput">{{ value }}</div>
  </div>
</template>

<script>
export default {
  name: "MyTag",
  data() {
    return {
      flag: false,
    };
  },
  methods: {
    showInput() {
      this.flag = true;
      this.$nextTick(() => {
        this.$refs.input.focus();
      });
    },
    close(e) {
      if (e.target.value.trim() === "") {
        alert("不能为空");
      } else {
        this.$emit("input", e.target.value);
      }
      this.flag = false;
    },
  },
  props: {
    value: {
      type: String,
    },
  },
  //   directives: {
  //     focus: {
  //       inserted(el) {
  //         el.focus();
  //       },
  //     },
  //   },
};
</script>

<style lang="less">
.my-tag {
  cursor: pointer;
  .input {
    appearance: none;
    outline: none;
    border: 1px solid #ccc;
    width: 100px;
    height: 40px;
    box-sizing: border-box;
    padding: 10px;
    color: #666;
    &::placeholder {
      color: #666;
    }
  }
}
</style>
